package testcases;

import static org.junit.Assert.assertEquals;

import com.trentech.billcalc.FinanceManager;
import com.trentech.billcalc.Resident;
import com.trentech.billcalc.event.ArriveEvent;
import com.trentech.billcalc.event.LeaveEvent;
import com.trentech.billcalc.event.RenewLeaseEvent;
import com.trentech.billcalc.event.TransferLeaseEvent;


public class LeaseTest 
{
	public static void testLease() throws Exception
	{
		FinanceManager fm = new FinanceManager();
        
        // Start new file
        // MainTest.startFile();
        // assertEquals(0, fm.getResidents().size());
        // assertEquals(0, fm.getEvents().size());
        
        // Arrive : Marie Putri,day-10 <as the owner of the apartment>
        EventData.SaveEvent(fm, EventData.ListArrive.get(0));
        // Arrive : Jessa,day-2 <as one of residents>
        EventData.SaveEvent(fm, EventData.ListArrive.get(2));     
        assertEquals(2, fm.getResidents().size());
        
        // Start Lease : Jessa day 0-30 to Marie Putri, 20000-10000-200, 2
        // Jessa owes Marie 1 full month + deposit : 20200
        EventData.SaveEvent(fm, EventData.ListStartLease.get(0));
        assertEquals(-20200, fm.getTotalOwing(fm.findResident("Marie Putri")));
        assertEquals(20200, fm.getTotalOwing(fm.findResident("Jessa")));
        
        // Arrive Nexia, day 4 <as one of residents>
        EventData.SaveEvent(fm, EventData.ListArrive.get(4));
        
        // Transfer Lease : Jessa day 6 to Nexia, true
        // Nexia owes Jessa 24 days + deposit : (24/30)*20000 + 200 : 16200
        EventData.SaveEvent(fm, EventData.ListTransferLease.get(0));
        assertEquals(4000, fm.getTotalOwing(fm.findResident("Jessa")));
        assertEquals(16200, fm.getTotalOwing(fm.findResident("Nexia")));
        
        // Arrive : Lappie, day-11
        EventData.SaveEvent(fm, EventData.ListArrive.get(3));
        
        // Transfer Lease : Nexia day 12 to Lappie, false
        // Lappie owes Marie Putri 18 days + deposit : (18/30) * 20000 + 200 : 12200 
        EventData.SaveEvent(fm, EventData.ListTransferLease.get(1));
        assertEquals(12200, fm.getTotalOwing(fm.findResident("Lappie")));
        assertEquals(-32400, fm.getTotalOwing(fm.findResident("Marie Putri")));
        
        // Renew Lease : Lappie, day 30-61
        EventData.SaveEvent(fm, EventData.ListRenewLease.get(1));
        assertEquals(12200, fm.getTotalOwing(fm.findResident("Lappie")));
        
        
        // Leave : Lappie, day 42
        // Marie Putri should give back deposit + 20 days : (19/31) * 20000 + 200
        // Lappie owes Marie Putri Lease Break Penalty : 10000
        EventData.SaveEvent(fm, EventData.ListLeave.get(2));
        assertEquals(9742, fm.getTotalOwing(fm.findResident("Lappie")));
        assertEquals(-29942, fm.getTotalOwing(fm.findResident("Marie Putri")));
        
        // Arrive : Alya, day -3
        EventData.SaveEvent(fm, EventData.ListArrive.get(1));
        
        // Start Lease : Alya day 31-60 to Marie Putri, 25000-10000-500, 1
        EventData.SaveEvent(fm, EventData.ListStartLease.get(2));
        assertEquals(500, fm.getTotalOwing(fm.findResident("Alya")));
        assertEquals(-30442, fm.getTotalOwing(fm.findResident("Marie Putri")));
        
        // Vacation : Alya day 42-61, does not change the lease
        EventData.SaveEvent(fm, EventData.ListStartVacation.get(1));
        EventData.SaveEvent(fm, EventData.ListStopVacation.get(1));
        assertEquals(500, fm.getTotalOwing(fm.findResident("Alya")));
        assertEquals(-30442, fm.getTotalOwing(fm.findResident("Marie Putri")));
	}
}
